#include <iostream>
#include <vector>
#include <BinaryTree.hpp>
#include <Vector.hpp>

using namespace std;

class Solution
{
public:
    vector<int> rightSideView(TreeNode *root)
    {
        vector<int> rv;
        if (root == nullptr)
            return vector<int>{};
        dfs(root, 0, rv);
        return rv;
    }

    void dfs(TreeNode *node, int level, vector<int> &rv)
    {
        if (rv.size() < level + 1)
        {
            rv.push_back(node->val);
        }
        else
        {
            rv[level] = node->val;
        }
        if (node->left)
        {
            dfs(node->left, level + 1, rv);
        }
        if (node->right)
        {
            dfs(node->right, level + 1, rv);
        }
    }
};
int main()
{
    cout << "Code199" << endl;
    Solution s;
    TreeNode *root = CreatTree("1,2,3,4,null,null,null,5", "null");
    auto res = s.rightSideView(root);
    PrintVector(res);
    return 0;
}